package main

import "fmt"

type CircleSingleLink struct {
	no   int
	name string
	next *CircleSingleLink
}

func ListCircleNode(head *CircleSingleLink) {
	temp := head
	for {
		if temp == head {
			temp = temp.next
		}
		fmt.Printf("[no=%v, name=%v] --> ", temp.no, temp.name)
		if temp.next == head {
			break
		}
		temp = temp.next
	}
}

func DeleteCircleNode(head *CircleSingleLink, id int) (newHead *CircleSingleLink) {
	newHead = head
	temp := head
	helper := head
	if temp.next == nil {
		fmt.Println("空环形节点")
		return
	}
	for {
		if helper.next == head {
			break
		}
		helper = helper.next
	}
	flag := false
	for {
		if temp.next == head {
			break
		}
		if temp.no == id {
			if temp == head {
				newHead = temp.next
			}
			helper.next = temp.next
			fmt.Printf("\n[%v, %v]删除节点\n", temp.no, temp.name)
			flag = true
			break
		}
		temp = temp.next
		helper = helper.next
	}
	if !flag {
		if temp.no == id {
			helper.next = temp.next
			fmt.Printf("\n[%v, %v]删除节点\n", temp.no, temp.name)
		} else {
			fmt.Printf("\n未找到no=%d\n", id)
		}
	}
	return
}
func InsertCircleNode(head *CircleSingleLink, newNode *CircleSingleLink) {
	temp := head
	for {
		if temp.next == nil {
			head.no = newNode.no
			head.name = newNode.name

			head.next = newNode
			newNode.next = head
			break
		}
		if temp.next == head {
			temp.next = newNode
			newNode.next = head
			break
		}
		temp = temp.next
	}
}
